﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class AB05Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string WBBTP;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        #endregion
        public AB05Report()
            : base()
        {
        }
        public AB05Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            ABPAUFFKRS = dicParamater["WBBAUFKRS"].ToString().Trim();
            ABPAUFFLFD = dicParamater["WBBAUFLFD"].ToString().Trim();
            ABPAUFFPOS = dicParamater["WBBAUFPOS"].ToString().Trim();
            WBBTP = dicParamater["WBBTP"].ToString().Trim();

            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        #region------------------------------Function------------------------



        private void FillInOrderPos(DataTable dtSource)
        {
            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();
                foreach (DataRow dwSource in dtSource.Rows)
                {

                    CellName = "FACTORY";
                    CellValue = dwSource["LSNAME1"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "FACTORY1";
                    CellValue = dwSource["LSNAME1"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "ARTICLE";
                    CellValue = dwSource["ABPART"].ToString().Trim() + '-'
                        + dwSource["ABPFARBE"].ToString().Trim() + '-'
                        + dwSource["ABPVAR"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "FINISH";
                    CellValue = dwSource["ABPBESCHICH"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "COLORDES";
                    CellValue = dwSource["COLORDES"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "COMPOSITION";
                    CellValue = dwSource["COMPOSITION"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "GREIGEWIDTH";
                    CellValue = dwSource["WARWBRT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "FINISHWIDTH";
                    CellValue = dwSource["WAFBRTL"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "STARTDATE";
                    //CellValue = dwSource["ABPSTARTDAT"].ToString().Trim();
                    CellValue = System.DateTime.Today.ToShortDateString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "ENDDATE";
                    CellValue = dwSource["ABPENDDAT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "PRICE";
                    CellValue = dwSource["PRICE"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "GREIGEQTY";
                    CellValue = dwSource["QTY"].ToString().Trim() + '匹' +
                        dwSource["GREIGEQTY"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "YARDQTY";
                    CellValue = dwSource["YARDQTY"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "CONTRACTION";
                    CellValue = dwSource["WALAEAEND"].ToString().Trim() + '%';
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "COMMENTS";
                    CellValue = dwSource["COMS"].ToString().Replace("        ", " ") + Environment.NewLine
                        + dwSource["REMARK"].ToString().Replace("        ", " ");
                    exAgent.SetCellValue(CellName, CellValue, true);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void FillInOrder(DataTable dtSource)
        {
            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();
                foreach (DataRow dwSource in dtSource.Rows)
                {

                    CellName = "COMORDER";
                    CellValue = dwSource["ABKKDZEICH"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "REMARK";
                    CellValue = dwSource["ABKAUFTTXT1"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "OPENDATE";
                    CellValue = dwSource["ABKAEDAT"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "STAFF";
                    CellValue = dwSource["HRNNAME"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void FillInLot(DataTable dtSource)
        {
            try
            {

                Queue CellQueue = new Queue();
                CellQueue.Clear();
                foreach (DataRow dwSource in dtSource.Rows)
                {

                    CellName = "ORDER";
                    CellValue = dwSource["ORDERNO"].ToString().Trim();
                    /*dwSource["WBBAUFKRS"].ToString().Trim()+'.'
                    + dwSource["WBBAUFLFD"].ToString().Trim() + '.'
                    + dwSource["WBBAUFPOS"].ToString().Trim() + '.'
                    + dwSource["WBBTP"].ToString().Trim();*/
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "LOT";
                    CellValue = dwSource["WBBDPART"].ToString().Trim();
                    exAgent.SetCellValue(CellName, CellValue, true);

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public void GetAB05Report(string fileType)
        {

            var query = from dwABPOS in dsSource.Tables["ABPOS"].AsEnumerable()
                        select dwABPOS;

            //string strSheetName;
            // string TableName = "";
            // int Rown = 0;
            // string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "Finishing";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\" + ABPAUFFKRS + ABPAUFFLFD + ABPAUFFPOS + ".xls";
                        sheetName = ABPAUFFKRS + "_" + ABPAUFFLFD + "_" + ABPAUFFPOS + "_" + WBBTP;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        CellName = "PrintDate";
                        value = System.DateTime.Today.ToShortDateString() + " ";
                        value += System.DateTime.Now.ToShortTimeString();
                        exAgent.SetCellValue(CellName, value, true);
                        CellName = "PageNum";
                        value = "1";
                        exAgent.SetCellValue(CellName, value, true);
                        FillInOrder(dsSource.Tables["ABKOPF"]);
                        FillInOrderPos(dsSource.Tables["ABPOS"]);
                        FillInLot(dsSource.Tables["LOT"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (strCompany == "HLT")
                {
                    // HideNullRow();
                }
                else
                {
                    //HideNullRow();
                }
                exAgent.Save();
                //MessageBox.Show("你是否打开Exclel?",,MessageBoxButtons.YesNo,MessageBoxDefaultButton.Button2);

                exAgent.Close();
            }
        }


        private void HideNullRow()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);
                var val2 = exAgent.GetCellValue(1, 1);
                var val3 = exAgent.GetCellValue(1, 1);
                //Case 1
                rowstart = 4; rowend = 15; col1 = 1;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 1, 11);
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col1);
                    }
                }
                /*  rowstart = 53; rowend = 56; col1 = 2; col2 = 3;
                  for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                  {
                      val1 = exAgent.GetRangeValue(Rowi, Rowi, 9, 38);
                      if ((string)val1 == "0")

                          //.get_Value(Type.Missing);
                          if ((string)val1 == "0")
                          {
                              HidedRows++;
                              exAgent.HideRows(Rowi, col1, Rowi, col1);
                          }
                  }
                 /* rowstart = 341; rowend = 351; col1 = 17; col2 = 23;
                  for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                  {
                      val1 = exAgent.GetRangeValue(Rowi, Rowi, 3, 4);
                      val2 = exAgent.GetRangeValue(Rowi, Rowi, 21, 22);
                      if ((string)val1 == "0" && (string)val2 == "0")
                      {
                          HidedRows++;
                          exAgent.HideRows(Rowi, col1, Rowi, col2);
                      }
                  }
                  /*
                               rowstart = 381 + HidedRows - 21; rowend = 387 + HidedRows - 21; col1 = 3; col2 = 22;
                               for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                               {
                                   val1 = exAgent.GetCellValue(Rowi, col1);
                                   val2 = exAgent.GetCellValue(Rowi, col2);
                                   if (val1 == null && val2 == null)
                                   {
                                       HidedRows++;
                                       exAgent.HideRows(Rowi, col1, Rowi, col2);
                                   }
                               }
                               */
                /*  int InsertedRows = HidedRows - 22;
                  if (HidedRows > 0)
                  {
                      rowstart = 359;
                      exAgent.InsertRows(rowstart, HidedRows - 22);
                  }
                  rowstart = 433 + InsertedRows; rowend = 1800 + InsertedRows;
                  for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                  {
                      val1 = exAgent.GetRangeValue(Rowi, Rowi, 8, 34);
                      if ((string)val1 == "0")
                      {
                          if (Rowi == rowstart)
                              Rowi = Rowi - 1;
                          exAgent.HideRows(Rowi, col1, rowend, col1);
                          Rowi = rowend;
                      }
                  }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
    }
}